package com.kly.cashmall.module.order.adapter;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.kly.cashmall.bean.LoanTypeBean;
import com.kly.cashmall.module.order.LoanListFragment;
import com.kly.cashmall.utils.function.BundleHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @date 2018/8/20
 */
public class LoanPagerAdapter extends FragmentStatePagerAdapter {

  private List<LoanTypeBean> mTypeList;
  private Map<Integer, LoanListFragment> mListFragmentMap = new HashMap<>();

  public LoanPagerAdapter(FragmentManager fm, List<LoanTypeBean> typeList) {
    super(fm);
    this.mTypeList = typeList;
  }

  @Override
  public Fragment getItem(int position) {
    LoanListFragment orderListFragment = new LoanListFragment();
    orderListFragment.setArguments(BundleHelper.create()
        .putSerializable(LoanListFragment.KEY_TYPE_ID, mTypeList.get(position).orderListName)
        .get());
    return orderListFragment;
  }

  @Override
  public int getCount() {
    return mTypeList.size();
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    LoanListFragment fragment = (LoanListFragment) super.instantiateItem(container, position);
    mListFragmentMap.put(position, fragment);
    return fragment;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    mListFragmentMap.remove(position);
    super.destroyItem(container, position, object);
  }

  public LoanListFragment getFragment(int index) {
    return mListFragmentMap.get(index);
  }
}
